

import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessionbeans.FriendshipBeanRemote;

/**
 * Servlet implementation class SendFriendshipServlet
 */
public class SendFriendshipServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SendFriendshipServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String receiver = request.getParameter("receiver");
		String userId = (String)request.getSession().getAttribute("userId");
		FriendshipBeanRemote friendshipBean;
		InitialContext context;
		System.out.print(receiver);
		if (receiver != null) {
			try {
				context = new InitialContext();
				Object ref = context.lookup("FriendshipBeanRemote");
				friendshipBean = (FriendshipBeanRemote) PortableRemoteObject.narrow(ref,FriendshipBeanRemote.class);
				friendshipBean.sendFriendshipRequest(userId,receiver);
			} catch (NamingException e) {
				System.out.println("Naming Exception");
				e.printStackTrace();
			}
		}
		response.sendRedirect("ProfileServlet?user=" + receiver);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
